package com.sugon.api;

import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.dao.DevMaintenWorkDao;
import com.sugon.dao.ModuleRepairRocordDao;
import com.sugon.entity.*;
import com.sugon.service.*;
import com.sugon.util.ApiBaseAction;
import com.sugon.util.ApiPageUtils;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "模具紧急维护接口")
@RestController
@RequestMapping("/api/ModuleRepairRocord")
public class ApiModuleRepairRocordController extends ApiBaseAction {

    @Autowired
    private ModuleArchivesService moduleArchivesService;
    @Autowired
    private ModuleRepairRocordService moduleRepairRocordService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private ModuleRepairRocordDao moduleRepairRocordDao;
    @Autowired
    private DevMaintenWorkDao devMaintenWorkDao;
    @Autowired
    private ModuleReplacePartService moduleReplacePartService;

    @AuthCheck
    @ApiOperation(value = "模具信息查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", dataType = "Integer", required = true),
    })
    @RequestMapping(value = "getDevList", method = RequestMethod.POST)
    public Object getModList(
            @LoginUser SysUserEntity loginUser,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size
    ) {


        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "CREATE_DATE");
        params.put("order", "DESC");
        //查询列表数据
        Query map = new Query(params);


        String userId = loginUser.getUserId();
        //获取当前部门id
        String deptId = loginUser.getDeptId();
        //获取用户是否是维修人员
        Integer leader = loginUser.getLeader();

        if (deptId.length() >= 4) {
            deptId = deptId.substring(0, 4);
            params.put("subShop", deptId);
        }
        if (deptId.length() > 3) {
            deptId = deptId.substring(0, 4);
            params.put("subShop", deptId);
        }


        List<ModuleArchivesEntity> moduleArchivesList = moduleArchivesService.queryListApi(params);
        int total = moduleArchivesService.queryTotal(params);
        ApiPageUtils pageUtil = new ApiPageUtils(moduleArchivesList, total, map.getLimit(), map.getPage());
        return toResponsSuccess(pageUtil);

    }

    @AuthCheck
    @ApiOperation(value = "模具紧急维护列表查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", dataType = "Integer", required = true),
    })
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Object list(@ApiIgnore @LoginUser SysUserEntity loginUser, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "20") Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "CREATE_DATE");
        params.put("order", "DESC");
        /*
            首先判断是否是模具维修组
                如果是
                 1、只可以看自己车间的
                 2、维修工只可以看自己的
                否则：不处理
         */
        String type = sysDeptService.getDeptTypeById(loginUser.getDeptId());
        if ("DT_MJWXZ".equals(type)) {
            //组长 查询所有
            params.put("workShop", loginUser.getWorkShopId());
            //当前为模具
            if (1 != loginUser.getLeader()) {
                //普通技术员,查询指派任务
                params.put("orderExecuteUser", loginUser.getUserId());
            }
        }

        //查询列表数据
        Query map = new Query(params);
        List<ModuleRepairRocordEntity> moduleRepairRocordList = moduleRepairRocordDao.queryList(map);
        int total = moduleRepairRocordDao.queryTotal(map);
        ApiPageUtils pageUtil = new ApiPageUtils(moduleRepairRocordList, total, map.getLimit(), map.getPage());
        return toResponsSuccess(pageUtil);
    }

    /**
     * 模具紧急维护获取模修人员
     *
     * @param
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "模具紧急维护获取模修人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "deptId", paramType = "query", dataType = "Integer", required = true),
    })
    @RequestMapping(value = "getRepairUser", method = RequestMethod.POST)
    public Object getRepairUser(
            @RequestParam(value = "deptId", required = false) String deptId
    ) {

        List userList = devMaintenWorkDao.getRepairUser(deptId);
        return toResponsSuccess(userList);

    }

    @AuthCheck
    @ApiOperation(value = "模具紧急维护完成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "userId", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "deptId", value = "deptId", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "deptId", required = false) String deptId
    ) {

        int flag = moduleRepairRocordService.endErrorApi(id, userId, deptId);
        return toResponsSuccess(flag);
    }

    /**
     * 模具紧急维护详情
     *
     * @param
     * @return
     */

    @AuthCheck
    @ApiOperation(value = "模具紧急维护详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public Object info(
            @RequestParam(value = "id", required = false) String id
    ) {

        ModuleRepairRocordEntity moduleRepairRocord = moduleRepairRocordService.queryObject(id);
        return toResponsSuccess(moduleRepairRocord);
    }

    /**
     * 模具紧急维护换件记录
     *
     * @param
     * @return
     */

    @AuthCheck
    @ApiOperation(value = "模具紧急维护换件纪录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "modulCode", value = "模具编码", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "moduleReplacePartList", method = RequestMethod.POST)
    public Object moduleReplacePartList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "modulCode", required = false) String modulCode
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("moduleCode", modulCode);
        params.put("page", page);
        params.put("limit", size);
        Query query = new Query(params);
        List<ModuleReplacePartEntity> moduleReplacePartList = moduleReplacePartService.queryList(query);
        int total = moduleReplacePartService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(moduleReplacePartList, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }

    @Override
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //CustomDateEditor为自定义日期编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
